package com.qinge.blog;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * @author QDW
 * @date 2022/4/27 17:03
 * @description 根据实体生成sevice
 **/
public class GenServiceByEntity {
    // 指定dao生成所在包的路径
    private String packageOutPath = "com.qinge.blog.service";
    // dao层
    private String daoPath="com.qinge.blog.dao";
    // 实体
    private String entityPath = "com.qinge.blog.entity";

    // 作者名字
    private String authorName = "QDW";


    public GenServiceByEntity(){
        List<String> classNames=new ArrayList<>();

        try {
            classNames=getClassName(entityPath);
        } catch (IOException e) {
            e.printStackTrace();
        }

        for(String className:classNames){
            generateService(className);
            generateServiceImpl(className);
        }
    }


    /**
     * @author QDW
     * @date 2022/4/27 16:14
     * @description 默认只有一层目录
     * @param packageName
     * @return List<String>
     **/
    public static List<String> getClassName(String packageName) throws IOException {
        List<String> fileNames = new ArrayList<>();
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        String packagePath = packageName.replace(".", "/");
        Enumeration<URL> urls = loader.getResources(packagePath);

        while (urls.hasMoreElements()) {

            URL url = urls.nextElement();
            if (url == null){
                continue;
            }

            String type = url.getProtocol();
            fileNames.addAll(getClassNameByFile(url.getPath()));
        }

        return fileNames;
    }

    /**
     * @author QDW
     * @date 2022/4/27 16:27
     * @description
     * @param filePath
     * @return java.util.List<java.lang.String>
     **/
    private static List<String> getClassNameByFile(String filePath)  {
        List<String> myClassName = new ArrayList<>();
        File file = new File(filePath);
        File[] childFiles = file.listFiles();

        if (childFiles == null) {
            return myClassName;
        }

        for (File childFile : childFiles) {
            String childFilePath = childFile.getName();
            if (childFilePath.endsWith(".class")) {
                myClassName.add(childFilePath.split("\\.")[0]);
            }
        }

        return myClassName;
    }


    /**
     * @author QDW
     * @date 2022/4/27 16:14
     * @description 根据类名生成dao
     * @param entityName
     * @return void
     **/
    private void generateService(String entityName){
        String content= String.format("package %s;\r\n\r\n" +
                "import com.baomidou.mybatisplus.extension.service.IService;\r\n" +
                "import %s.%s;\r\n\r\n" +
                "/**\r\n" +
                " * @author %s\r\n"+
                " * @date %s\r\n"+
                " * @description %s service层类\r\n"+
                " */ \r\n"+
                "public interface %sService extends IService<%s> {\r\n\r\n}\r\n",
                packageOutPath,
                entityPath, entityName,
                this.authorName,
                getSysdate(),
                entityName, entityName, entityName);

        File directory = new File("");

        String outputPath = directory.getAbsolutePath() + "\\blog-service\\src\\main\\java\\" + this.packageOutPath.replace(".", "\\")+"\\";

        write(outputPath, entityName +"Service.java",content);
    }



    /**
     * @author QDW
     * @date 2022/4/27 16:14
     * @description 根据类名生成dao
     * @param entityName
     * @return void
     **/
    private void generateServiceImpl(String entityName){
        String content= String.format("package %s.impl;\r\n\r\n" +
                "import org.springframework.stereotype.Service;\r\n"+
                "import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;\r\n" +
                "import %s.%sDao;\r\n" +
                "import %s.%sService;\r\n" +
                "import %s.%s;\r\n\r\n" +
                "/**\r\n" +
                " * @author %s\r\n"+
                " * @date %s\r\n"+
                " * @description %s service层实现类\r\n"+
                " */ \r\n"+
                "@Service\r\n"+
                "public class %sServiceImpl extends ServiceImpl<%sDao,%s>  implements %sService {\r\n\r\n}\r\n",
                packageOutPath,
                daoPath,entityName,
                packageOutPath,entityName,
                entityPath, entityName,
                this.authorName,
                getSysdate(),
                entityName, entityName, entityName,entityName,entityName);

        File directory = new File("");

        String outputPath = directory.getAbsolutePath() + "\\blog-service\\src\\main\\java\\" + this.packageOutPath.replace(".", "\\")+"\\impl\\";

        write(outputPath, entityName +"ServiceImpl.java",content);
    }


    private void write(String path,String fileName,String content){
        try {
            File file = new File(path);
            file.mkdirs();

            FileWriter fw = new FileWriter(path+fileName);
            PrintWriter pw = new PrintWriter(fw);

            pw.println(content);

            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取系统当前时间
     *
     * @param
     * @return
     * @explain 使用Date实现
     */
    public static String getSysdate() {
        Date date = new Date(System.currentTimeMillis());

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return dateFormat.format(date);
    }

    public static void main(String[] args) {
        new GenServiceByEntity();
    }
}
